package net.awpspace.mobile.global;

import java.util.Vector;

/**
 * Persistent database (USER table only)
 * 
 * @author Diep
 * 
 */
public class Database {

	/**
	 * Singleton
	 */
	private static Database instance;

	private Vector usernames;
	private Vector passwords;

	private Database() {
		usernames = new Vector();
		passwords = new Vector();
	}

	public static Database getInstance() {
		if (instance == null) {
			instance = new Database();
		}
		return instance;
	}

	/**
	 * Check username and password match
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean checkLogin(String username, String password) {
		for (int i = 0; i < usernames.size(); ++i) {
			if (usernames.elementAt(i).toString().equals(username)
					&& passwords.elementAt(i).toString().equals(password)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Register a new user
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean checkRegister(String username, String password) {
		for (int i = 0; i < usernames.size(); ++i) {
			if (usernames.elementAt(i).toString().equals(username)) {
				return false;
			}
		}
		usernames.addElement(username);
		passwords.addElement(password);
		return true;
	}
}
